package com.android.fastride.model.db;

import android.app.Application;

import com.android.fastride.model.db.greendao.DaoMaster;
import com.android.fastride.model.db.greendao.DaoSession;


/**
 * 数据库管理
 * Created by pengyuantao on 2017/10/16 9:00.
 */

public class GreenDaoManager {

	//数据库名称
	public static final String DB_NAME = "cs_db";

	//主要进行操作的Session
	private DaoSession daoSession;

	public static class Holder {
		static GreenDaoManager INSTANCE = new GreenDaoManager();
	}

	public static GreenDaoManager getInstance () {
		return Holder.INSTANCE;
	}

	/**
	 * 初始化数据库
	 *
	 * @param application
	 */
	public void init (Application application) {
		ControlSystemSQLiteHelper systemSQLiteHelper = new ControlSystemSQLiteHelper(application, DB_NAME);
		DaoMaster daoMaster = new DaoMaster(systemSQLiteHelper.getWritableDb());
		daoSession = daoMaster.newSession();
	}

	/**
	 * 返回操作数据库的Session类
	 * @return
	 */
	public DaoSession getDaoSession(){
		if (daoSession == null) {
			throw new IllegalStateException("You must init GreenDaoManager first!");
		}
		return daoSession;
	}
}
